﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Web;
using System.Web.Mvc;
using QAS.Nekorama.Models;
using PagedList;

namespace QAS.Nekorama.Controllers
{
    public class QuestionController : Controller
    {
        //
        // GET: /Question/

        private Entities db = new Entities();
        public ActionResult Index(string sortOrder, int? page)
        {
            ViewBag.CurrentSort = sortOrder;
            ViewBag.NewestSortParm = "Newest";
            ViewBag.FrequentSortParm = "Frequent";
            ViewBag.VotesSortParm = "Votes";
            ViewBag.FeaturedSortParm = "Featured";
            ViewBag.UnansweredSortParm = "Unanswered";
            //if (searchString != null)
            //{
            //    page = 1;
            //}
            //else
            //{
            //    searchString = currentFilter;
            //}

            //ViewBag.CurrentFilter = searchString;
            var questionList = from q in db.Questions
                            select q;
            switch (sortOrder)
            {
                case "Newest":
                    questionList = questionList.OrderByDescending(q => q.CreatedDate);
                    break;
                case "Frequent":
                    questionList = questionList.OrderByDescending(q => q.ViewCount);
                    break;
                case "Votes":
                    questionList = questionList.OrderByDescending(q => q.VoteCount);
                    break;
                case "Featured":
                    questionList = questionList.Where(q => q.BountyID != null);
                    break;
                case "Unanswered":
                    questionList = questionList.OrderByDescending(q => q.CreatedDate);
                    break;
                default:
                    questionList = questionList.OrderByDescending(q => q.CreatedDate);
                    break;
            }
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            return View(questionList.ToPagedList(pageNumber, pageSize));
        }

        //
        // GET: /Question/Details/5

        public ActionResult Details(int id)
        {
            var model = new QuestionDetailModel();
            var db = new Entities();
            var res = from r in db.Questions.AsEnumerable()
                      where (r.QuestionID == id)
                      select r;
            var ans = (from a in db.Answers.AsEnumerable()
                       where (a.QuestionID == id)
                       select a);
            
            var cmm = (from c in db.Comments.AsEnumerable()
                       where (c.QuestionID == id)
                       select c);
            //model.Answers = ans.ToList();
            //model.Comments = cmm.ToList();
            var ansm= from a in ans join u in db.Users on a.CreatedUser equals u.UserID
                      select
             
            
            model.Question = res.ToList()[0];
            return View(model);
        }

        //
        // GET: /Question/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Question/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Question/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Question/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Question/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Question/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
